// pages/doctor/doctor.js
import { getDoctorList, formatDate, login } from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    show:false,
    dateList:[],
    currentDate:0,
    doctorList: [],
    minDate:"",
    maxDate: ""
  },
  onCalendar() {
      this.setData({
        show:true
      })
  },
  onClose() {
    this.setData({ show: false });
  },
  
  onConfirm(e){
    let currentDate = formatDate(new Date(e.detail))
    this.setData({
      currentDate: currentDate
    })
    this.dateArr(currentDate)
    this.getData()
    this.setData({ show: false });
  },
  dateArr(currentDate){
    const weekArr=['周日','周一','周二','周三','周四','周五','周六']
    let temp = new Date().getTime()
    if (currentDate && new Date(currentDate).getTime() > (new Date().getTime() + 86400 * 1000 * 4) && (new Date(currentDate).getTime() + 86400 * 1000 * 4)<this.data.maxDate){
      temp = new Date(currentDate).getTime()
    } else if ((new Date(currentDate).getTime() + 86400 * 1000 * 4) >= this.data.maxDate){
      temp = this.data.maxDate - 86400 * 1000 * 4
    }
    let arr=[]
    for(let i=0;i<5;i++){
      let nowDate = new Date(temp + 86400 * 1000 * i)
      arr.push({
        id:i,
        week: weekArr[nowDate.getDay()],
        date: nowDate.getMonth() + 1 + '/' + nowDate.getDate(),
        fullDate: formatDate(nowDate)
      })
    }
    this.setData({
      dateList: arr
    })
  },
  selectDate(e){
    this.setData({
      currentDate: e.currentTarget.dataset.info.fullDate
    })    
    this.getData()
  },
  getData(){
    getDoctorList({ date: this.data.currentDate}).then(res => {
      this.setData({
        minDate: new Date(res.data.allow_order_date[0]).getTime(),
        maxDate: new Date(res.data.allow_order_date.pop()).getTime(),
        doctorList: res.data.list
      })
    })
  },
  register(e) {
    // if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/appointment/appointment?id=' + e.currentTarget.dataset.id + '&date=' + this.data.currentDate,
      })
    // } else {
    //   login(false, '/pages/appointment/appointment?id=' + e.currentTarget.dataset.id + '&date=' + this.data.currentDate)
    // }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.dateArr()
    let date=new Date()
    let nowdate = formatDate(date)
    this.setData({
      currentDate: nowdate
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.getStorageSync('model3')) {
      wx.setNavigationBarTitle({
        title: wx.getStorageSync('model3')
      })
    }
    if (wx.getStorageSync('isLogin')) {
      this.setData({
        isLogin: wx.getStorageSync('isLogin')
      })
    } else {
      this.setData({
        isLogin: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})